package car.slyc.controller;


import car.slyc.model.Yyjl;
import car.slyc.service.YyjlServiceImpl;
import car.slyc.util.JsonObject;
import car.slyc.util.PageRet;
import car.slyc.util.Time;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/yyjl")
public class YyjlController {

    @Resource
    private YyjlServiceImpl yyjlService;


    //    查询所有   ----前端
    @RequestMapping("/qt/wuxgetall")
    @ResponseBody
    public List<Yyjl> wuxgetall(@RequestParam(value = "uid",required = false,defaultValue = "") String uid ,
                                @RequestParam(value = "type",required = false,defaultValue = "") String type,
                                @RequestParam(value = "xcid",required = false,defaultValue = "") String xcid){
        System.out.println("用户id:"+uid);
        System.out.println("分类:"+type);
        List<Yyjl>  list=yyjlService.getallmore(uid,type,xcid);
        return list;
    }

//     新增
    @RequestMapping("/qt/wuxadd")
    @ResponseBody
    public JsonObject wuxadd(Yyjl yyjl) {
        yyjl.setId(UUID.randomUUID().toString().replace("-", ""));
        yyjl.setTime(Time.getDate());
        if (yyjlService.add(yyjl) > 0) {
            return new JsonObject("200", "", null);
        } else {
            return new JsonObject("500", "", null);
        }
    }




    //   删除
    @RequestMapping("/qt/wuxdel")
    @ResponseBody
    public JsonObject wuxdel(@RequestParam(value = "id",defaultValue = "",required = false) String id){

        try {
            if (yyjlService.del(id) > 0) {

                return new JsonObject("200", "删除成功", null);
            } else {
                return new JsonObject("500", "删除失败", null);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            return new JsonObject("500", "删除失败", null);
        }
    }

    //   获取详情
    @RequestMapping("/qt/wuxgetinfo")
    @ResponseBody
    public JsonObject wuxgetinfo(@RequestParam(value = "id",defaultValue = "",required = false) String id){

        try {
            Yyjl yyjl=yyjlService.getById(id);
            return new JsonObject("200", "", yyjl);
        }
        catch (Exception e){
            e.printStackTrace();
            return new JsonObject("500", "", null);
        }
    }






    //
    @RequestMapping("/tolist")
    public String tolist() {
        return "Yyjl/list";
    }

    //    显示
    @RequestMapping("/tolistxj")
    public String tolistxj() {
        return "Yyjl/listxj";
    }


    //   跳转 添加
    @RequestMapping("/toadd")
    public String toadd(Model model) {

        model.addAttribute("modyid", "");

        return "Yyjl/add";
    }


    //   跳转 修改
    @RequestMapping("/toget")
    public String toget(String id, Model model) {
        System.out.println("修改的数据" + id);
        model.addAttribute("modyid", id);

        return "Yyjl/add";
    }

    //   添加
    @RequestMapping("/add")
    @ResponseBody
    public PageRet add(Yyjl yyjl) {
        yyjl.setId(UUID.randomUUID().toString().replace("-", ""));
        System.out.println(Time.getDate());
        yyjl.setTime(Time.getDate());
        if (yyjlService.add(yyjl)>0){
            return new PageRet(200, "", null, 0);
        }else{
            return new PageRet(500, "", null, 0);
        }
    }

    //   修改
    @RequestMapping("/get")
    @ResponseBody
    public JsonObject getById(String id) {

        Yyjl yyjl = yyjlService.getById(id);
        System.out.println(yyjl);

        return new JsonObject("200", "", yyjl);
    }


    //   修改
    @RequestMapping("/update")
    @ResponseBody
    public PageRet update(Yyjl yyjl) {
        System.out.println("前端传输的数据：" + yyjl);
        if (yyjlService.update(yyjl)>0){
            return new PageRet(200, "", null, 0);
        }else{
            return new PageRet(500, "", null, 0);
        }
    }


    //   修改
    @RequestMapping("/update_state")
    @ResponseBody
    public PageRet update_state(@RequestParam("id") String id, @RequestParam("state") String state) {

        System.out.println("id=：" + id + "&state=" + state);
        if (yyjlService.update_state(id, state) > 0) {
            return new PageRet(200, "", null, 0);
        } else {
            return new PageRet(500, "", null, 0);
        }
    }

    //   批量删除
    @RequestMapping("/del")
    @ResponseBody
    public PageRet dels(@RequestParam("id") String ids) {

        System.out.println("传递过来的数据："+ids.toString());

        if (yyjlService.dels(ids)>0){
            return new PageRet(200, "成功", null, 0);
        }
        else{
            return new PageRet(500, "失败", null, 0);
        }
    }


    //   分页显示所有
    @RequestMapping(value = "/pagegetall")
    @ResponseBody
    public PageRet list(@RequestParam(value = "page", defaultValue = "1", required = false) int page,
                        @RequestParam(value = "limit", defaultValue = "10", required = false) int limit
    ) {

        //  查询之前调用    传入 当前页码、每页条数  及排序方法
        PageHelper.startPage(page, limit, " time desc");
        //startPage 后面紧跟的查询是分页查询
        List<Yyjl> list=yyjlService.getall("");
        //用PageInfo 对结果进行包装，传入连续显示的页数
        PageInfo<Yyjl> all = new PageInfo<>(list);
        System.out.println("pageinfo包装后的数据" + all.getList());
        return new PageRet(0, "", all.getList(), (int) all.getTotal());
    }


    //   分页显示所有
    @RequestMapping(value = "/pagegetallxj")
    @ResponseBody
    public PageRet listxj(@RequestParam(value = "page", defaultValue = "1", required = false) int page,
                        @RequestParam(value = "limit", defaultValue = "10", required = false) int limit
    ) {

        //  查询之前调用    传入 当前页码、每页条数  及排序方法
        PageHelper.startPage(page, limit, " time desc");
        //startPage 后面紧跟的查询是分页查询
        List<Yyjl> list=yyjlService.getallxj("");
        //用PageInfo 对结果进行包装，传入连续显示的页数
        PageInfo<Yyjl> all = new PageInfo<>(list);
        System.out.println("pageinfo包装后的数据" + all.getList());
        return new PageRet(0, "", all.getList(), (int) all.getTotal());
    }

}
